package Encoder;

import java.lang.reflect.Array;

public class Coder {
	
	String password = "1234";
	
	public Coder() {
		
	}
	
	public Coder(String password) {
		this.password = password;
	}
		
	public String encode(String text, String pw) {
		String textToModify = text;
		byte[] bytesOfTheText = textToModify.getBytes();
		byte[] bytesOfTheSeed = pw.getBytes();
		int[] ints = new int[bytesOfTheText.length];
		textToModify = new String("");

		for(int i = 0; i < bytesOfTheText.length; i++) {
			bytesOfTheText[i] += bytesOfTheSeed[i%bytesOfTheSeed.length];
			ints[i] = bytesOfTheText[i];
			textToModify += (-1)*ints[i]+"O";
		}
		
		return textToModify;
	}
	
	public String decode(String text, String pw) {
		String textToModify = text;
		String[] textParts = textToModify.split("O");
		int[] ints = new int[textParts.length];
		byte[] bytesOfTheSeed = pw.getBytes();
		byte[] bytesOfTheText = new byte[ints.length];
		
		for(int i = 0; i < bytesOfTheText.length; i++) {
			ints[i] = (-1)*Integer.parseInt(textParts[i]);
			bytesOfTheText[i] = (byte)ints[i];
			bytesOfTheText[i] -= bytesOfTheSeed[i%bytesOfTheSeed.length];
		}
		
		textToModify = new String(bytesOfTheText);
		return textToModify;
	}

	public void setPassword(String password) {
		this.password = password;
	}
	
	public String getPassword() {
		return password;
	}
}
